package xyz.bali16.module.activeuser.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.starblues.bootstrap.annotation.AutowiredType;
import com.gitee.starblues.spring.SpringBeanFactory;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.bali16.application.core.model.UserVo;
import xyz.bali16.application.core.utils.IDUtils;
import xyz.bali16.application.entity.User;
import xyz.bali16.application.service.UserService;
import xyz.bali16.module.activeuser.entity.ActiveUser;
import xyz.bali16.module.activeuser.entity.UserActiveUser;
import xyz.bali16.module.activeuser.mapper.ActiveUserMapper;
import xyz.bali16.module.activeuser.mapper.UserActiveUserMapper;
import xyz.bali16.module.activeuser.model.ActiveUserAddParam;
import xyz.bali16.module.activeuser.model.ActiveUserPageParam;

import java.util.List;


/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@Service
public class ActiveUserServiceImpl extends ServiceImpl<ActiveUserMapper, ActiveUser> implements ActiveUserService {
    private final ActiveUserMapper activeUserMapper;
    private final UserActiveUserMapper userActiveUserMapper;

    @Autowired
    @AutowiredType(AutowiredType.Type.PLUGIN_MAIN)
    private UserService userService;

    public ActiveUserServiceImpl(ActiveUserMapper activeUserMapper, @AutowiredType(AutowiredType.Type.PLUGIN_MAIN) UserActiveUserMapper userActiveUserMapper) {
        this.activeUserMapper = activeUserMapper;
        this.userActiveUserMapper = userActiveUserMapper;
    }

    @Override
    public IPage<ActiveUser> getPage(ActiveUserPageParam param) {
        IPage<?> page = new Page<>(param.getCurrent(), param.getSize());
        Wrapper<UserActiveUser> wrapper = Wrappers.<UserActiveUser>lambdaQuery().eq(UserActiveUser::getUserId, param.getUserId());
        UserActiveUser userActiveUser = userActiveUserMapper.selectOne(wrapper);
        String activeUserId = null;
        if (userActiveUser != null) {
            activeUserId = userActiveUser.getActiveUserId();
        }
        return this.baseMapper.getPage(page, activeUserId);
    }

    public ActiveUser initActiveUser(String userId) {
        ActiveUser activeUser = new ActiveUser();
        activeUser.setId(IDUtils.uuid());
        activeUser.setUserId(userId);
        UserActiveUser userActiveUser = new UserActiveUser();
        userActiveUser.setUserId(userId);
        userActiveUser.setActiveUserId(activeUser.getId());
        userActiveUserMapper.insert(userActiveUser);
        return activeUser;
    }

    @Override
    public void addActiveUser(ActiveUserAddParam param) throws Exception {
        Preconditions.checkNotNull(param, "ActiveUserAddParam 参数不能为为空");
        ActiveUser activeUser = new ActiveUser();
        activeUser.setId(IDUtils.uuid());
        activeUser.setUserId(param.getUserId());
        BeanUtils.copyProperties(param, activeUser);


        UserActiveUser userActiveUser = new UserActiveUser();
        userActiveUser.setUserId(param.getUserId());
        userActiveUser.setActiveUserId(activeUser.getId());

        userActiveUserMapper.insert(userActiveUser);
        save(activeUser);
        System.out.println(activeUser);


    }

    @Override
    public void updateActiveUser(ActiveUserAddParam param) throws Exception {
        Preconditions.checkNotNull(param, "ActiveUserAddParam 参数不能为为空");
        ActiveUser activeUser = new ActiveUser();
        BeanUtils.copyProperties(param, activeUser);
        Wrapper<UserActiveUser> wrapper = Wrappers.<UserActiveUser>lambdaQuery().eq(UserActiveUser::getUserId, param.getUserId());
        UserActiveUser userActiveUser = userActiveUserMapper.selectOne(wrapper);
        activeUser.setId(userActiveUser.getActiveUserId());
        updateById(activeUser);
    }

    @Override
    @Transactional
    public void delete(String activeUserId) throws Exception {
        if (activeUserId == null) {
            return;
        }
        Wrapper<UserActiveUser> wrapper = Wrappers.<UserActiveUser>lambdaQuery().eq(UserActiveUser::getActiveUserId, activeUserId);
        userActiveUserMapper.deleteById(wrapper);
        removeById(activeUserId);
    }

    @Override
    public void addPostNum(String userId) {
        Wrapper<UserActiveUser> wrapper = Wrappers.<UserActiveUser>lambdaQuery().eq(UserActiveUser::getUserId, userId);
        UserActiveUser userActiveUser = userActiveUserMapper.selectOne(wrapper);
        if (userActiveUser != null) {
            String activeUserId = userActiveUser.getActiveUserId();
            ActiveUser activeUser = activeUserMapper.selectById(activeUserId);
            activeUser.setPostNum(activeUser.getPostNum() + 1);
            activeUserMapper.updateById(activeUser);
            updateScore(activeUser);
        } else {
            ActiveUser activeUser = initActiveUser(userId);
            activeUser.setPostNum(1L);
            save(activeUser);
        }
    }

    @Override
    public void addCollectNum(String userId) {

        Wrapper<UserActiveUser> wrapper = Wrappers.<UserActiveUser>lambdaQuery().eq(UserActiveUser::getUserId, userId);
        UserActiveUser userActiveUser = userActiveUserMapper.selectOne(wrapper);
        if (userActiveUser != null) {
            String activeUserId = userActiveUser.getActiveUserId();
            ActiveUser activeUser = activeUserMapper.selectById(activeUserId);
            activeUser.setCollectNum(activeUser.getCollectNum() + 1);
            activeUserMapper.updateById(activeUser);
            updateScore(activeUser);
        } else {
            ActiveUser activeUser = initActiveUser(userId);
            activeUser.setCollectNum(1L);
            save(activeUser);
        }

    }

    @Override
    public void addLikeNum(String userId) {
        Wrapper<UserActiveUser> wrapper = Wrappers.<UserActiveUser>lambdaQuery().eq(UserActiveUser::getUserId, userId);
        UserActiveUser userActiveUser = userActiveUserMapper.selectOne(wrapper);
        if (userActiveUser != null) {
            String activeUserId = userActiveUser.getActiveUserId();
            ActiveUser activeUser = activeUserMapper.selectById(activeUserId);
            activeUser.setLikeNum(activeUser.getLikeNum() + 1);
            activeUserMapper.updateById(activeUser);
            updateScore(activeUser);
        } else {
            ActiveUser activeUser = initActiveUser(userId);
            activeUser.setLikeNum(1L);
            save(activeUser);
        }

    }

    @Override
    public void addReplyNum(String userId) {
        Wrapper<UserActiveUser> wrapper = Wrappers.<UserActiveUser>lambdaQuery().eq(UserActiveUser::getUserId, userId);
        UserActiveUser userActiveUser = userActiveUserMapper.selectOne(wrapper);
        if (userActiveUser != null) {
            String activeUserId = userActiveUser.getActiveUserId();
            ActiveUser activeUser = activeUserMapper.selectById(activeUserId);
            activeUser.setReplyNum(activeUser.getReplyNum() + 1);
            activeUserMapper.updateById(activeUser);
            updateScore(activeUser);
        } else {
            ActiveUser activeUser = initActiveUser(userId);
            activeUser.setReplyNum(1L);
            save(activeUser);
        }
    }

    @Override
    public void removePostNum(String userId) {
        Wrapper<UserActiveUser> wrapper = Wrappers.<UserActiveUser>lambdaQuery().eq(UserActiveUser::getUserId, userId);
        UserActiveUser userActiveUser = userActiveUserMapper.selectOne(wrapper);
        String activeUserId = userActiveUser.getActiveUserId();
        ActiveUser activeUser = activeUserMapper.selectById(activeUserId);
        activeUser.setPostNum(activeUser.getPostNum() - 1);
        activeUserMapper.updateById(activeUser);
        updateScore(activeUser);
    }

    @Override
    public void removeCollectNum(String userId) {
        Wrapper<UserActiveUser> wrapper = Wrappers.<UserActiveUser>lambdaQuery().eq(UserActiveUser::getUserId, userId);
        UserActiveUser userActiveUser = userActiveUserMapper.selectOne(wrapper);
        String activeUserId = userActiveUser.getActiveUserId();
        ActiveUser activeUser = activeUserMapper.selectById(activeUserId);
        activeUser.setCollectNum(activeUser.getCollectNum() - 1);
        activeUserMapper.updateById(activeUser);
        updateScore(activeUser);
    }

    @Override
    public void removeLikeNum(String userId) {
        Wrapper<UserActiveUser> wrapper = Wrappers.<UserActiveUser>lambdaQuery().eq(UserActiveUser::getUserId, userId);
        UserActiveUser userActiveUser = userActiveUserMapper.selectOne(wrapper);
        String activeUserId = userActiveUser.getActiveUserId();
        ActiveUser activeUser = activeUserMapper.selectById(activeUserId);
        activeUser.setLikeNum(activeUser.getLikeNum() - 1);
        activeUserMapper.updateById(activeUser);
        updateScore(activeUser);
    }

    @Override
    public void removeReplyNum(String userId) {
        Wrapper<UserActiveUser> wrapper = Wrappers.<UserActiveUser>lambdaQuery().eq(UserActiveUser::getUserId, userId);
        UserActiveUser userActiveUser = userActiveUserMapper.selectOne(wrapper);
        String activeUserId = userActiveUser.getActiveUserId();
        ActiveUser activeUser = activeUserMapper.selectById(activeUserId);
        activeUser.setReplyNum(activeUser.getReplyNum() - 1);
        activeUserMapper.updateById(activeUser);
        updateScore(activeUser);
    }

    /**
     * 此处，发帖与回复是占比最高的
     * 收藏与点赞比较低比例
     *
     * @param activeUser
     */
    @Override
    public void updateScore(ActiveUser activeUser) {
        Long score = (activeUser.getReplyNum() * 35 + activeUser.getCollectNum() * 3 + activeUser.getLikeNum() * 2 + activeUser.getPostNum() * 60) / 100;
        activeUser.setScore(score);
        activeUserMapper.updateById(activeUser);
    }
    @Override
    public List<ActiveUser> getRank(Integer currPage,Integer pageSize){
        ActiveUserPageParam activeUserPageParam = new ActiveUserPageParam();
        activeUserPageParam.setCurrent(currPage);
        activeUserPageParam.setSize(pageSize);
        IPage<ActiveUser> activeUserIPage = this.getPage(activeUserPageParam);
        List<ActiveUser> activeUserList = activeUserIPage.getRecords();
        for (ActiveUser activeUser : activeUserList) {
            String userId = activeUser.getUserId();
            User user = userService.getById(userId);
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(user, userVo);
            activeUser.setUserVo(userVo);
        }
        return activeUserList;
    }

}
